package com.boss.client;

import com.boss.common.EncryptionUtils;
import com.boss.common.FileUtils;

import java.io.*;
import java.net.Socket;
import java.util.Scanner;

/**
 * @author 郑炆杰
 */
public class Client {
    private static final String SERVER_ADDRESS = "localhost";
    private static final int SERVER_PORT = 12345;
    private BufferedReader in;
    private PrintWriter out;

    public static void main(String[] args) {
        Client client = new Client();
        client.start();
    }

    public void start() {
        try {
            Socket socket = new Socket(SERVER_ADDRESS, SERVER_PORT);
            in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            out = new PrintWriter(socket.getOutputStream(), true);

            new Thread(new ServerListener()).start();

            Scanner scanner = new Scanner(System.in);
            boolean condition = true;
            while (condition) {
                System.out.println("请选择操作：");
                System.out.println("1. 发送文件给其他客户端");
                System.out.println("2. 发送文件给服务端");
                System.out.println("3. 退出客户端");

                int choice = scanner.nextInt();

                switch (choice) {
                    case 1:
                        System.out.println("请输入目标客户端ID和文件路径：");
                        String clientMessage = scanner.nextLine();
                        // 发送消息给其他客户端的逻辑
                        String input = scanner.nextLine();
                        if (input.startsWith("send")) {
                            sendFile(input);
                        } else if (input.startsWith("show")) {
                            out.println(input);
                            // 发送 show 命令到服务器
                        } else {
                            out.println(input);
                        }
                        break;
                    case 2:
                        System.out.println("请输入文件路径：");
                        scanner.nextLine();
                        String serverMessage = scanner.nextLine();
                        out.println(serverMessage);
                        break;
                    case 3:
                        System.out.println("客户端已退出。");
                        condition = false;
                        break;
                    default:
                        System.out.println("无效选项，请重新选择。");
                }
            }
            System.exit(0);
        } catch (IOException e) {
            System.out.println("无法连接到服务器: " + e.getMessage());
        }
    }
    private void sendFile(String inputLine) {
        try {
            String[] parts = inputLine.split(" ");
            String filePath = parts[2];
            String fileContent = FileUtils.readFile(filePath);
            String encryptedContent = EncryptionUtils.encrypt(fileContent);
            out.println(inputLine);
            out.println(encryptedContent);
        } catch (Exception e) {
            System.out.println("Error sending file: " + e.getMessage());
        }
    }
    private class ServerListener implements Runnable {
        @Override
        public void run() {
            try {
                String message;
                while ((message = in.readLine()) != null) {
                    System.out.println(message);
                }
            } catch (IOException e) {
                System.out.println("Error reading from server: " + e.getMessage());
            }
        }
    }
}
